package com.xtianzhuang.www.controller.project;

import java.util.ArrayList;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xtianzhuang.www.annotation.Log;
import com.xtianzhuang.www.annotation.Log.LogType;
import com.xtianzhuang.www.annotation.LogOperate;
import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.TdPProjectParam;
import com.xtianzhuang.www.entity.TdPProjectParamExample;
import com.xtianzhuang.www.entity.constants.CodeConstants;
import com.xtianzhuang.www.entity.request.DeleteProjectParamRequest;
import com.xtianzhuang.www.entity.request.ProjectParamListRequest;
import com.xtianzhuang.www.entity.request.SaveProjectParamRequest;
import com.xtianzhuang.www.entity.response.DeleteProjectParamResponse;
import com.xtianzhuang.www.entity.response.ProjectParamListResponse;
import com.xtianzhuang.www.entity.response.SaveProjectParamResponse;
import com.xtianzhuang.www.utils.PageData;

/**
 * 
 * @author xtian.zhuang
 * @date Jul 22, 2019
 */
@Controller
@RequestMapping("/project")
public class ProjectParamController extends BaseController {
	@RequestMapping("/projectParamList")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	public ProjectParamListResponse projectParamList(ProjectParamListRequest request) {
		ProjectParamListResponse response = new ProjectParamListResponse();
		TdPProjectParamExample example = new TdPProjectParamExample();
		if (request.getProjectId() != null) {
			example.or().andProjectIdEqualTo(request.getProjectId());
			example.setOffset(request.getOffset());
			example.setLimit(request.getLimit());
			PageData<TdPProjectParam> data = commonServiceBiz.getPageData(example);
			data.getData().add(new TdPProjectParam());
			response.setCode(CodeConstants.SUCCESS);
			response.setData(data.getData());
			response.setTotal(data.getTotal());
		} else {
			response.setCode(CodeConstants.SUCCESS);
			response.setData(new ArrayList<TdPProjectParam>());
			response.setTotal(0);
		}
		return response;

	}
	@RequestMapping("/ajaxSaveProjectParam")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	@LogOperate
	public SaveProjectParamResponse saveProjectParam(SaveProjectParamRequest request) {
		SaveProjectParamResponse response = new SaveProjectParamResponse();
		try {
			if (request.getId() == null) {
				projectServiceBiz.addProjectParam(request);
				response.setCode(CodeConstants.SUCCESS);
				response.setMessage("添加成功");
			} else {
				projectServiceBiz.updateProjectParam(request);
				response.setCode(CodeConstants.SUCCESS);
				response.setMessage("修改成功");
			}

		} catch (Exception e) {
			e.printStackTrace();
			response.setMessage(e.getMessage());
			response.setCode("9999");
		}
		return response;
	}

	@RequestMapping("/ajaxDeleteProjectParam")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	@LogOperate
	public DeleteProjectParamResponse deleteProjectParam(DeleteProjectParamRequest request) {
		DeleteProjectParamResponse response = new DeleteProjectParamResponse();
		try {
			projectServiceBiz.deleteProjectParam(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			response.setMessage(e.getMessage());
			response.setCode("9999");
		}
		return response;
	}
}
